package com.yxhl.zoume.common.ui.activity.base;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.core.main.info.HeaderOperationType;
import com.yxhl.zoume.utils.DensityUtils;

import butterknife.BindView;


/**
 * Created by Xboy on 16/2/24.
 */
public abstract class BaseActivityWithToolBar extends BaseActivity {

    @BindView(R.id.appbar_include_root)
    AppBarLayout mAppBarLayout;

    @BindView(R.id.toolbar_include_head)
    Toolbar mToolbar;

    /**
     * toolbar返回键
     */
    @BindView(R.id.iv_head_back)
    ImageView mHeadBack;
    /**
     * toolbar标题
     */
    @BindView(R.id.tv_head_title)
    TextView mHeadTitle;

    /**
     * toolbar右侧文本
     */
    @BindView(R.id.tv_head_right)
    TextView mHeadRight;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initializeToolBar();
    }


    private void initializeToolBar() {
        //Android 4.4以上状态栏透明(在代码里直接声明透明状态栏更有效)
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);

            mToolbar.setMinimumHeight(DensityUtils.dip2px(this, 73));
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) mToolbar.getLayoutParams();
            marginLayoutParams.setMargins(0, DensityUtils.dip2px(this, 25), 0, 0);
        }

        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        mHeadBack.setOnClickListener(new onToolbarNavigationClickListener());
        mHeadRight.setOnClickListener(new onHeadRightClickListener());
    }

    public void setToolbarVisible(boolean visible) {
        if (visible) {
            mAppBarLayout.setVisibility(View.VISIBLE);
        } else {
            mAppBarLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 设置Toolbar的左侧图标,居中标题内容,右侧文本内容
     *
     * @param resId
     * @param headTitle
     * @param operate
     */
    public void setToolbar(@DrawableRes int resId, String headTitle, String operate) {
        if (resId != 0) {
            mHeadBack.setImageResource(resId);
        }
        if (headTitle != null) {
            mHeadTitle.setText(headTitle);
        }
        if (operate != null) {
            mHeadRight.setText(operate);
        }
    }

    /**
     * 设置Toolbar的左侧图标,居中标题内容,右侧文本内容
     *
     * @param resId
     * @param headTitle
     * @param operate
     */
    public void setToolbar(@DrawableRes int resId, @StringRes int headTitle, @StringRes int operate) {
        if (resId != 0) {
            mHeadBack.setImageResource(resId);
        }
        if (headTitle != 0) {
            mHeadTitle.setText(headTitle);
        }
        if (operate != 0) {
            mHeadRight.setText(operate);
        }
    }

    /**
     * 设置Toolbar的左侧图标,居中标题内容,右侧文本内容
     *
     * @param resId
     * @param headTitle
     * @param operate
     */
    public void setToolbar(@DrawableRes int resId, @StringRes int headTitle, String operate) {
        if (resId != 0) {
            mHeadBack.setImageResource(resId);
        }
        if (headTitle != 0) {
            mHeadTitle.setText(headTitle);
        }
        if (operate != null) {
            mHeadRight.setText(operate);
        }
    }

    /**
     * 设置Toolbar的左侧图标,居中标题内容,右侧图标
     *
     * @param resId
     * @param headTitle
     * @param rightResId
     */
    public void setToolbar(@DrawableRes int resId, String headTitle, @DrawableRes int rightResId) {
        if (resId != 0) {
            mHeadBack.setImageResource(resId);
        }
        if (headTitle != null) {
            mHeadTitle.setText(headTitle);
        }

        mHeadRight.setText("");
        mHeadRight.setBackgroundResource(rightResId);
    }

    /**
     * 设置Toolbar的左侧图标,居中标题内容,右侧文本内容
     *
     * @param drawable
     * @param headTitle
     * @param operate
     */
    public void setToolbar(Drawable drawable, String headTitle, String operate) {

        mHeadBack.setImageDrawable(drawable);

        if (headTitle != null) {
            mHeadTitle.setText(headTitle);
        }
        if (operate != null) {
            mHeadRight.setText(operate);
        }
    }


    /**
     * 设置Toolbar标题内容和右侧文本
     *
     * @param headTitle
     * @param operate
     */
    public void setToolbar(String headTitle, String operate) {
        setToolbar(0, headTitle, operate);
    }

    /**
     * 设置Toolbar标题内容和右侧图标
     *
     * @param headTitle
     * @param operation
     */
    public void setToolbar(@StringRes int headTitle, String operation) {
        setToolbar(0, headTitle, operation);
    }

    /**
     * 设置Toolbar标题内容和右侧图标
     *
     * @param headTitle
     * @param resId
     */
    public void setToolbar(String headTitle, @DrawableRes int resId) {
        setToolbar(0, headTitle, resId);
    }


    /**
     * 设置Toolbar标题
     *
     * @param headTitle
     */
    public void setToolbarTitle(String headTitle) {
        setToolbar(0, headTitle, "");
    }

    /**
     * 设置Toolbar标题
     *
     * @param headTitleRes
     */
    public void setToolbarTitle(@StringRes int headTitleRes) {
        setToolbar(0, headTitleRes, "");
    }

    /**
     * 设置Toolbar导航图标(返回图标)
     *
     * @param drawable
     */
    public void setLeftIcon(Drawable drawable) {
        mHeadBack.setImageDrawable(drawable);
    }

    /**
     * 设置Toolbar导航图标(返回图标)
     *
     * @param resId
     */
    public void setLeftIcon(@DrawableRes int resId) {
        mHeadBack.setImageResource(resId);
    }

    /**
     * 设置Toolbar右侧图标
     *
     * @param resId
     */
    public void setRightIcon(@DrawableRes int resId) {
        mHeadRight.setText("");
        mHeadRight.setBackgroundResource(resId);
    }

    /**
     * 设置toolbar右侧文本
     *
     * @param operation
     */
    public void setRightOperation(String operation) {
        mHeadRight.setText(operation);
    }

    /**
     * 设置Toolbar右侧图标
     *
     * @param drawable
     */
    public void setRightIcon(Drawable drawable) {
        mHeadRight.setText("");
        if (Build.VERSION.SDK_INT < 16) {
            mHeadRight.setBackgroundDrawable(drawable);
        } else {
            mHeadRight.setBackground(drawable);
        }

    }

    /**
     * 隐藏Toolbar导航图标(返回图标)
     */
    public void hideLeftIcon() {
        mHeadBack.setImageDrawable(null);
    }

    /**
     * 隐藏Toolbar导航图标后重新显示(返回图标)
     */
    public void showLeftIcon() {
        mHeadBack.setImageDrawable(getResources().getDrawable(R.drawable.ic_back));
    }

    public void setHeaderRightTag(HeaderOperationType operationType) {
        if (mHeadRight != null) {
            mHeadRight.setTag(operationType);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * Toolbar返回图标的点击响应
     */
    protected abstract void onHeaderLeftClick(View view);

    /**
     * Toolbar右侧文本的点击响应
     */
    protected abstract void onHeaderRightClick(View view);

    /**
     * 获取toolbar用于popupwindow确定现实位置
     * @return
     */
    public Toolbar getToolbar () {
        return mToolbar;
    }



    protected class onToolbarNavigationClickListener implements View.OnClickListener {

        @Override
        public void onClick(final View v) {
            if (v != null) {
                Animation animation = AnimationUtils.loadAnimation(BaseActivityWithToolBar.this, R.anim.alpha_exit);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        onHeaderLeftClick(v);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                v.startAnimation(animation);
            }
        }
    }

    protected class onHeadRightClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (v != null) {
                onHeaderRightClick(v);
            }
        }
    }
}
